//
//  RegisteredViewController.m
//  ChatApp
//
//  Created by DJL on 15/12/20.
//  Copyright © 2015年 DJL. All rights reserved.
//

#import "RegisteredViewController.h"
#import <SMS_SDK/SMSSDK.h>
#import "EMError.h"
#import "EaseMob.h"
@interface RegisteredViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneNumber;
@property (weak, nonatomic) IBOutlet UIButton *send;
@property (weak, nonatomic) IBOutlet UITextField *verificationCode;
@property (weak, nonatomic) IBOutlet UIButton *next;
@property (weak, nonatomic) IBOutlet UIView *codeVeiw;
@property (nonatomic, strong)NSTimer *timer;
@property (nonatomic, assign)NSInteger num;
@end

@implementation RegisteredViewController
//发送验证码
- (IBAction)sendAction:(id)sender {
    self.send.enabled = NO;
    self.codeVeiw.alpha = 1;
//    self.timer = [NSTimer timerWithTimeInterval:1.f target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];
    [SMSSDK getVerificationCodeByMethod:0 phoneNumber:self.phoneNumber.text zone:@"+86" customIdentifier:nil result:^(NSError *error) {
        
        
    }];

}

- (void)handleTimer:(NSTimer *)timer {

    [self.send setTitle:[NSString stringWithFormat:@"重新发送(%ld)",60 - self.num] forState:UIControlStateNormal];
    self.num ++;
    
    if (self.num == 60) {
        [self.timer  invalidate];
        [self.send setTitle:@"发送验证码"forState:UIControlStateNormal];
        self.send.enabled = YES;
        self.num = 0;
        self.codeVeiw.alpha = 0;
    }
    
}

//下一步
- (IBAction)nextAction:(id)sender {
    
    [SMSSDK commitVerificationCode:self.verificationCode.text phoneNumber:self.phoneNumber.text zone:@"+86" result:^(NSError *error) {
        
        if (!error) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"设置密码" preferredStyle:UIAlertControllerStyleAlert ];
            /* 创建action对象 */
            
            [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
                
                
                textField.placeholder =@"添加密码";
            }];
            
            UIAlertAction *actionConfirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                
                UITextField *text = alert.textFields.firstObject;
                if ([text.text length] == 6) {
//                    /* 通过单例获取通知中心对象 */
//                    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//                    
//                    [center postNotificationName:@"用户信息" object:nil userInfo:@{passWord : text.text}];
                    
                    
                    
                    [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.phoneNumber.text password:text.text withCompletion:^(NSString *username, NSString *password, EMError *error) {
                        if (!error) {
                            NSLog(@"注册成功");
                            
                            [self dismissViewControllerAnimated:YES completion:^{
                                
                                
                            }];
                        } else {
                            /* 创建AlertController对象 */
                            
                            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@" 警告" message:[NSString  stringWithFormat:@"%@",error] preferredStyle:UIAlertControllerStyleAlert ];
                            /* 创建action对象 */
                            
                            UIAlertAction *actionConfirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                                
                                
                                
                            }];
                            /* alert添加action对象 */
                            [alert addAction:actionConfirm];
                            /* 推出 */
                            [self presentViewController:alert animated:YES completion:^{
                                
                                
                            }];
                            
                            
                            
                            
                        }
                    } onQueue:nil];
                    
                    
                    
                    
                    
                } else {
                    
                    
                    /* 创建AlertController对象 */
                    
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"密码长度为6位数" preferredStyle:UIAlertControllerStyleAlert ];
                    /* 创建action对象 */
                    
                    UIAlertAction *actionConfirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                        
                        
                        
                    }];
                    /* alert添加action对象 */
                    [alert addAction:actionConfirm];
                    /* 推出 */
                    [self presentViewController:alert animated:YES completion:^{
                        
                        
                    }];
                    
                    
                    
                    
                }
                

                
                
                
            }];
            /* alert添加action对象 */
            [alert addAction:actionConfirm];
            /* 推出 */
            [self presentViewController:alert animated:YES completion:^{
                
                
            }];
            

            
            
            
        }
        else
        {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"验证失败" preferredStyle:UIAlertControllerStyleAlert ];
                       UIAlertAction *actionConfirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                
                           
                
                
            }];
            /* alert添加action对象 */
            [alert addAction:actionConfirm];
            /* 推出 */
            [self presentViewController:alert animated:YES completion:^{
                
                
            }];
            
        }
        
    }];
    

    
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    [self.timer invalidate];
    self.timer = nil;
    
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:YES];
    self.codeVeiw.alpha = 0;
    
}
//返回
- (IBAction)back:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
